package me.leon.keeplive

import android.content.Context
import android.graphics.Bitmap
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import kotlin.math.roundToInt
import me.leon.keeplive.databinding.LayoutSettingItemLibBinding

class SettingItemView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
    RelativeLayout(context, attrs) {

    var binding: LayoutSettingItemLibBinding =
        LayoutSettingItemLibBinding.bind(
            View.inflate(context, R.layout.layout_setting_item_lib, this)
        )
    init {
        initStyle(attrs)
    }

    private fun initStyle(attrs: AttributeSet?) {
        binding.run {
            val a = context.obtainStyledAttributes(attrs, R.styleable.SettingItemView)

            val rightMore = a.getResourceId(R.styleable.SettingItemView_setting_right_more_icon, -1)
            val leftIcon = a.getResourceId(R.styleable.SettingItemView_setting_left_icon, -1)
            val title = a.getString(R.styleable.SettingItemView_setting_name)
            val detail = a.getString(R.styleable.SettingItemView_setting_detail)
            val content = a.getString(R.styleable.SettingItemView_setting_content)
            val titleColor = a.getColor(R.styleable.SettingItemView_setting_name_color, -1)
            val title2 = a.getResourceId(R.styleable.SettingItemView_setting_name, -1)
            val detail2 = a.getResourceId(R.styleable.SettingItemView_setting_detail, -1)
            val content2 = a.getResourceId(R.styleable.SettingItemView_setting_content, -1)

            val hideBorder = a.getBoolean(R.styleable.SettingItemView_setting_hide_border, true)
            val hideMore = a.getBoolean(R.styleable.SettingItemView_setting_hide_more, false)
            if (titleColor != -1) {
                tvSettingName.setTextColor(titleColor)
            }

            vBorder.visibility = if (!hideBorder) View.VISIBLE else View.GONE
            ivSettingRight.visibility = if (!hideMore) View.VISIBLE else View.GONE

            if (rightMore != -1) setRightMore(rightMore)
            if (leftIcon != -1) setLeftIcon(leftIcon)

            if (title2 != -1) setName(title2)
            if (detail2 != -1) setDetail(detail2)
            if (content2 != -1) setRightTxt(content2)
            if (!title.isNullOrEmpty()) setName(title)
            if (!detail.isNullOrEmpty()) setDetail(detail)
            if (!content.isNullOrEmpty()) setRightTxt(content)
            if (detail2 == -1 && detail.isNullOrEmpty()) tvSettingDetail.visibility = View.GONE

            a.recycle()
        }
    }

    fun setName(name: Int) {
        binding.tvSettingName.setText(name)
    }

    fun setName(name: String?) {
        binding.tvSettingName.text = name
    }
    fun setDetail(name: String?) {
        binding.tvSettingDetail.text = name
    }
    fun setDetail(name: Int) {
        binding.tvSettingDetail.setText(name)
    }

    private fun setLeftDrawable(@DrawableRes res: Int) {
        binding.tvSettingName.setCompoundDrawables(
            resources.getDrawable(res, null).apply { setBounds(0, 0, minimumWidth, minimumHeight) },
            null,
            null,
            null
        )
    }

    fun setRightTxt(@StringRes name: Int) {
        binding.tvSettingContent.setText(name)
    }

    fun getRightTxt() = binding.tvSettingContent.text.toString()
    fun getRightTextView() = binding.tvSettingContent
    fun getSettingNameView() = binding.tvSettingName

    fun setRightTxt(name: String?) {
        binding.tvSettingContent.text = name
    }

    fun setLeftIcon(@DrawableRes leftIcon: Int) {
        binding.ivSettingLeft.visibility = View.VISIBLE
        binding.ivSettingLeft.setImageResource(leftIcon)
    }

    fun setRightMore(rightMore: Int) {
        binding.ivSettingRight.setImageResource(rightMore)
        if (rightMore == -1) {
            val param: LayoutParams = binding.ivSettingRight.layoutParams as LayoutParams
            param.width = 0
            param.marginEnd = 0

            binding.ivSettingRight.layoutParams = param
        } else {
            val param: LayoutParams = binding.ivSettingRight.layoutParams as LayoutParams
            param.width = -2
            param.marginEnd = (resources.displayMetrics.density * 15 + 0.5).toInt()
            binding.ivSettingRight.layoutParams = param
        }
    }

    fun setRightMore(bitmap: Bitmap?) {
        binding.ivSettingRight.setImageBitmap(bitmap)
        if (bitmap == null) {
            val param: LayoutParams = binding.ivSettingRight.layoutParams as LayoutParams
            param.width = 0
            param.marginEnd = 0

            binding.ivSettingRight.layoutParams = param
        }
    }

    fun setRightMoreVisable(boolean: Boolean) {
        binding.ivSettingRight.visibility = if (boolean) View.VISIBLE else View.GONE
    }

    fun hideMore(isHide: Boolean) {
        if (isHide) setRightMore(null) else setRightMore(R.drawable.ic_detail)
        isEnabled = !isHide
    }

    fun hideBorder(isHide: Boolean) {
        binding.vBorder.visibility = if (isHide) View.GONE else View.VISIBLE
    }

    fun setBorderMargin(dp: Int) {
        val px = resources.displayMetrics.density * dp
        val param: LayoutParams = binding.vBorder.layoutParams as LayoutParams
        param.marginStart = px.roundToInt()
        param.marginEnd = px.roundToInt()
        binding.vBorder.layoutParams = param
    }
}
